package com.yw.atp.action;   
import java.io.File;  
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;  
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yw.atp.model.Version;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.util.ImageUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("fileUploadAction")
public class FileUploadAction extends ActionBase implements ServletRequestAware{  
	@Autowired
	private PersistenceService service;
	HttpServletRequest request;
	private static final long serialVersionUID = 1L;
    // 文件标题  
    private String title;  
    // 上传文件域对象  
    private File eicp;  
    // 上传文件名  
    private String eicpFileName;  
    // 上传文件类型  
    private String eicpContentType;  
	// 保存文件的目录路径(通过依赖注入)  
    private String savePath ="/upload";  
    private Version version;
    private String res;
    
	public String upload() throws Exception {
		try{
			if(version!=null&&version.getVersion()!=null&&eicp!=null){
				version.setUrl(ImageUtil.saveFile(eicp,ServletActionContext.getServletContext().getRealPath(savePath),eicpFileName));
				service.save(version);
				List<Version> v = service.findAll(Version.class,0,1);
				res = "提交新版本成功!当前版本号:"+v.get(0).getVersion();
			}
			return SUCCESS;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		res = "提交新版本失败!";
        return SUCCESS;  
    }  
    
	public String version(){
		try {
			List<Version> vs = service.findAll(Version.class,0,1);
			Version currentVersion = vs.get(0);
			JSONObject jo = new JSONObject();
			jo.put("version",currentVersion.getVersion());
			jo.put("url",currentVersion.getUrl());
			res = jo.toString();
			return SUCCESS;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		res = "{}";
		return SUCCESS;
	}
	
    public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public File getEicp() {
		return eicp;
	}

	public void setEicp(File eicp) {
		this.eicp = eicp;
	}

	public String getEicpFileName() {
		return eicpFileName;
	}

	public void setEicpFileName(String eicpFileName) {
		this.eicpFileName = eicpFileName;
	}

	public String getEicpContentType() {
		return eicpContentType;
	}

	public void setEicpContentType(String eicpContentType) {
		this.eicpContentType = eicpContentType;
	}
	
	public Version getVersion() {
		return version;
	}

	public void setVersion(Version version) {
		this.version = version;
	}
	
	public PersistenceService getService() {
		return service;
	}

	public void setService(PersistenceService service) {
		this.service = service;
	}
	
	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}
} 